//====================================================================
// Filename:      client.cc
//                
// It belongs to goblin-os.
//                
// Version:       
// Author:        Ins <InsFous@gmail.com>
// Created at:    Fri Dec 10 13:54:06 2010
// Description:   
//                
//====================================================================
#include "thrift/Queue.h"

#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using goblin::sqs::thrift::Message;
using goblin::sqs::thrift::QueueFlag;
using goblin::sqs::thrift::QueueStatus;

int main(int argc, char **argv) {
  boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
  boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
  boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));


  goblin::sqs::thrift::QueueClient client(protocol);
  transport->open();

  goblin::sqs::thrift::QueueName queue_name("testing");
  //QueueFlag::type queue_flag = QueueFlag::UsingExists;

  QueueFlag::type queue_flag = QueueFlag::UsingExists;

  client.CreateQueue(queue_name, queue_flag);

  client.SendMessage(queue_name, std::string("Hello, Ins!"));
  client.SendMessage(queue_name, std::string("Hello, Test!"));
  client.SendMessage(queue_name, std::string("Hello, Hello"));
  QueueStatus queue_status;
  Message message;
  /*
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);


  client.FetchMessage(message, queue_name);
  printf("MSG Id:%lld\n", message.msg_id);
  printf("MSG Content:%s\n", message.msg_content.c_str());
  printf("MSG Type:%d\n", message.msg_type);

  client.SetQueueCapacity(queue_name, 1000000);
  
  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);

  
  //client.SetQueueFilter(queue_name, 100);

  client.FetchMessage(message, queue_name);
  if (message.msg_id == -1) {
    printf("Not Got Message\n");
  } else {
    printf("Got Message, MSGID:%lld\n", message.msg_id);
  }

  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);

  client.SendMessageWithType(queue_name, std::string("InsFocus@gmail.com"), 100);

  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);


  client.FetchMessage(message, queue_name);
  printf("MSG Id:%lld\n", message.msg_id);
  printf("MSG Content:%s\n", message.msg_content.c_str());
  printf("MSG Type:%d\n", message.msg_type);

  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);


  client.RestoreQueueDefaultFilter(queue_name);
  client.SendMessageWithType(queue_name, std::string("Hello,world!"), 1000);

  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);
  */
  client.FetchMessage(message, queue_name);
  printf("MSG Id:%lld\n", message.msg_id);
  printf("MSG Content:%s\n", message.msg_content.c_str());
  printf("MSG Type:%d\n", message.msg_type);

  printf("****************************Status*****************************\n");
  client.GetQueueStatus(queue_status, queue_name);
  printf("Queue Name : %s\n", queue_status.queue_name.c_str());
  printf("Queue Capacity : %lld\n", queue_status.capacity);
  printf("Queue Unread : %lld messages\n", queue_status.unread_msg_count);
  printf("Queue Total : %lld messages\n", queue_status.total_msg_count);

  transport->close();
  return 0;
}
